/* eslint-disable prefer-promise-reject-errors */
import axios from "axios";
import { Toast } from "vant";

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL, // api 的 base_url
  timeout: 2000 // request timeout
});

// request 拦截
service.interceptors.request.use(
  config => {
    config.headers["usertoken"] = window.localStorage.getItem("usertoken");
    return config;
  },
  err => Promise.reject(err)
);

// response 拦截
service.interceptors.response.use(
  response => {
    return response;
  },
  error => {
    Toast.fail(error.response.data);
    return Promise.reject(error);
  }
);

export default service;
